﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;
using StockAdjust;

namespace PresentationLayer
{
    public partial class StockAdjustmentApproved : System.Web.UI.Page
    {
        AdjustmentRepository adjustmentRepo;
        ApproveStockAdjustmentVoucherControl control;

        protected void Page_Load(object sender, EventArgs e)
        {
            adjustmentRepo = new AdjustmentRepository();
            control = new ApproveStockAdjustmentVoucherControl();

            if (!IsPostBack)
            {
                lblMessage.Text = "!IsPostBack";
                GenerateGridView();
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            string redirectURL = control.ReturnToStockAdjustmentVoucherList();
            Response.Redirect(redirectURL);
        }

        private void GenerateGridView()
        {
            List<int> voucherId = Session["AdjustmentDetail"] as List<int>;
            if (voucherId != null)
            {
                List<AdjustmentDetail> adjList = new List<AdjustmentDetail>();
                if (voucherId != null)
                {
                    for (int i = 0; i < voucherId.Count; i++)
                    {
                        List<AdjustmentDetail> tempList = new List<AdjustmentDetail>();
                        tempList = control.DisplayAdjustmentDetail(voucherId[i]);
                        for (int j = 0; j < tempList.Count; j++)
                        {
                            adjList.Add(tempList[j]);
                        }
                    }
                }

                List<AdjustmentApproved> list = new List<AdjustmentApproved>();
                for (int i = 0; i < adjList.Count; i++)
                {
                    if (list.Count <= 0)
                    {
                        AdjustmentApproved adjApprove = new AdjustmentApproved();
                        adjApprove.ItemName = adjList[i].Stationery.ItemName;
                        adjApprove.AdjustQuantity = (int)adjList[i].Quantity;
                        adjApprove.UpdatedQuantity = adjList[i].Stationery.AvailableQty;
                        list.Add(adjApprove);
                    }
                    else
                    {
                        bool result = false;

                        for (int j = 0; j < list.Count; j++)
                        {
                            if (list[j].ItemName.Equals(adjList[i].Stationery.ItemName))
                            {
                                list[j].AdjustQuantity = list[j].AdjustQuantity + (int)adjList[i].Quantity;
                                result = true;
                                j = list.Count;
                            }
                        }
                        if (result == false)
                        {
                            AdjustmentApproved adjApprove = new AdjustmentApproved();
                            adjApprove.ItemName = adjList[i].Stationery.ItemName;
                            adjApprove.AdjustQuantity = (int)adjList[i].Quantity;
                            adjApprove.UpdatedQuantity = adjList[i].Stationery.AvailableQty;
                            list.Add(adjApprove);
                        }
                    }
                }

                gvAdjustApprove.DataSource = list;
                gvAdjustApprove.DataBind();

                lblMessage.Text = "The stock for the following items had been adjusted accordingly";
                Session.Remove("AdjustmentDetail");
            }
            else
            {
                lblMessage.Text = "No stock adjustment voucher has been selected for approval";
                gvAdjustApprove.Visible = false;
            }
        }
    }
}